/**
 *
 */
import Util;
import OSS;
import OpenPlatform;
import OSSUtil;
import FileForm;
import OpenApi;
import OpenApiUtil;
import EndpointUtil;

extends OpenApi;


init(config: OpenApi.Config){
  super(config);
  @endpointRule = '';
  
  checkConfig(config);
  @endpoint = getEndpoint('imagesearch', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }
  
  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model CommodityTitleRequest {
  instanceName?: string(name='InstanceName'),
  picContent?: string(name='PicContent'),
  titleIndex?: long(name='TitleIndex'),
}

model CommodityTitleAdvanceRequest {
  instanceName?: string(name='InstanceName'),
  picContentObject?: readable(name='PicContent'),
  titleIndex?: long(name='TitleIndex'),
}

model CommodityTitleResponseBody = {
  code?: int32(name='Code'),
  data?: {
    title?: string(name='Title'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model CommodityTitleResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: CommodityTitleResponseBody(name='body'),
}

async function commodityTitleWithOptions(request: CommodityTitleRequest, runtime: Util.RuntimeOptions): CommodityTitleResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  if (!Util.isUnset(request.titleIndex)) {
    body['TitleIndex'] = request.titleIndex;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'CommodityTitle',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function commodityTitle(request: CommodityTitleRequest): CommodityTitleResponse {
  var runtime = new Util.RuntimeOptions{};
  return commodityTitleWithOptions(request, runtime);
}

async function commodityTitleAdvance(request: CommodityTitleAdvanceRequest, runtime: Util.RuntimeOptions): CommodityTitleResponse {
  // Step 0: init client
  var accessKeyId = @credential.getAccessKeyId();
  var accessKeySecret = @credential.getAccessKeySecret();
  var securityToken = @credential.getSecurityToken();
  var credentialType = @credential.getType();
  var openPlatformEndpoint = @openPlatformEndpoint;
  if(Util.isUnset(openPlatformEndpoint)) {
    openPlatformEndpoint ='openplatform.aliyuncs.com';
  }
  if(Util.isUnset(credentialType)) {
    credentialType ='access_key';
  }
  var authConfig = new OpenApi.Config{
    accessKeyId = accessKeyId,
    accessKeySecret = accessKeySecret,
    securityToken = securityToken,
    type = credentialType,
    endpoint = openPlatformEndpoint,
    protocol = @protocol,
    regionId = @regionId,
  };
  var authClient = new OpenPlatform(authConfig);
  var authRequest = new OpenPlatform.AuthorizeFileUploadRequest{
    product = 'ImageSearch',
    regionId = @regionId,
  };
  var authResponse = new OpenPlatform.AuthorizeFileUploadResponse{};
  var ossConfig = new OSS.Config{
    accessKeySecret = accessKeySecret,
    type = 'access_key',
    protocol = @protocol,
    regionId = @regionId,
  };
  var ossClient : OSS = null;

  var fileObj = new FileForm.FileField{};
  var ossHeader = new OSS.PostObjectRequest.header{};
  var uploadRequest = new OSS.PostObjectRequest{};
  var ossRuntime = new OSSUtil.RuntimeOptions{};
  OpenApiUtil.convert(runtime, ossRuntime);
  var commodityTitleReq = new CommodityTitleRequest{};
  OpenApiUtil.convert(request, commodityTitleReq);

  if(!Util.isUnset(request.picContentObject)) {
    authResponse = authClient.authorizeFileUploadWithOptions(authRequest, runtime);
    ossConfig.accessKeyId = authResponse.body.accessKeyId;
    ossConfig.endpoint = OpenApiUtil.getEndpoint(authResponse.body.endpoint, authResponse.body.useAccelerate, @endpointType);
    ossClient = new OSS(ossConfig);

    fileObj = new FileForm.FileField{
      filename = authResponse.body.objectKey,
      content = request.picContentObject,
      contentType = '',
    };
    ossHeader = new OSS.PostObjectRequest.header{
      accessKeyId = authResponse.body.accessKeyId,
      policy = authResponse.body.encodedPolicy,
      signature = authResponse.body.signature,
      key = authResponse.body.objectKey,
      file = fileObj,
      successActionStatus = '201',
    };
    uploadRequest = new OSS.PostObjectRequest{
      bucketName = authResponse.body.bucket,
      header = ossHeader,
    };
    ossClient.postObject(uploadRequest, ossRuntime);
    commodityTitleReq.picContent = `http://${authResponse.body.bucket}.${authResponse.body.endpoint}/${authResponse.body.objectKey}`;
  }
  var commodityTitleResp = commodityTitleWithOptions(commodityTitleReq, runtime);
  return commodityTitleResp;
}

model GeneralRecognitionRequest {
  instanceName?: string(name='InstanceName'),
  picContent?: string(name='PicContent'),
}

model GeneralRecognitionAdvanceRequest {
  instanceName?: string(name='InstanceName'),
  picContentObject?: readable(name='PicContent'),
}

model GeneralRecognitionResponseBody = {
  code?: int32(name='Code'),
  data?: {
    regions?: [ string ](name='Regions'),
    result?: [ 
      {
        score?: string(name='Score'),
        tag?: string(name='Tag'),
      }
    ](name='Result'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model GeneralRecognitionResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: GeneralRecognitionResponseBody(name='body'),
}

async function generalRecognitionWithOptions(request: GeneralRecognitionRequest, runtime: Util.RuntimeOptions): GeneralRecognitionResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'GeneralRecognition',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function generalRecognition(request: GeneralRecognitionRequest): GeneralRecognitionResponse {
  var runtime = new Util.RuntimeOptions{};
  return generalRecognitionWithOptions(request, runtime);
}

async function generalRecognitionAdvance(request: GeneralRecognitionAdvanceRequest, runtime: Util.RuntimeOptions): GeneralRecognitionResponse {
  // Step 0: init client
  var accessKeyId = @credential.getAccessKeyId();
  var accessKeySecret = @credential.getAccessKeySecret();
  var securityToken = @credential.getSecurityToken();
  var credentialType = @credential.getType();
  var openPlatformEndpoint = @openPlatformEndpoint;
  if(Util.isUnset(openPlatformEndpoint)) {
    openPlatformEndpoint ='openplatform.aliyuncs.com';
  }
  if(Util.isUnset(credentialType)) {
    credentialType ='access_key';
  }
  var authConfig = new OpenApi.Config{
    accessKeyId = accessKeyId,
    accessKeySecret = accessKeySecret,
    securityToken = securityToken,
    type = credentialType,
    endpoint = openPlatformEndpoint,
    protocol = @protocol,
    regionId = @regionId,
  };
  var authClient = new OpenPlatform(authConfig);
  var authRequest = new OpenPlatform.AuthorizeFileUploadRequest{
    product = 'ImageSearch',
    regionId = @regionId,
  };
  var authResponse = new OpenPlatform.AuthorizeFileUploadResponse{};
  var ossConfig = new OSS.Config{
    accessKeySecret = accessKeySecret,
    type = 'access_key',
    protocol = @protocol,
    regionId = @regionId,
  };
  var ossClient : OSS = null;

  var fileObj = new FileForm.FileField{};
  var ossHeader = new OSS.PostObjectRequest.header{};
  var uploadRequest = new OSS.PostObjectRequest{};
  var ossRuntime = new OSSUtil.RuntimeOptions{};
  OpenApiUtil.convert(runtime, ossRuntime);
  var generalRecognitionReq = new GeneralRecognitionRequest{};
  OpenApiUtil.convert(request, generalRecognitionReq);

  if(!Util.isUnset(request.picContentObject)) {
    authResponse = authClient.authorizeFileUploadWithOptions(authRequest, runtime);
    ossConfig.accessKeyId = authResponse.body.accessKeyId;
    ossConfig.endpoint = OpenApiUtil.getEndpoint(authResponse.body.endpoint, authResponse.body.useAccelerate, @endpointType);
    ossClient = new OSS(ossConfig);

    fileObj = new FileForm.FileField{
      filename = authResponse.body.objectKey,
      content = request.picContentObject,
      contentType = '',
    };
    ossHeader = new OSS.PostObjectRequest.header{
      accessKeyId = authResponse.body.accessKeyId,
      policy = authResponse.body.encodedPolicy,
      signature = authResponse.body.signature,
      key = authResponse.body.objectKey,
      file = fileObj,
      successActionStatus = '201',
    };
    uploadRequest = new OSS.PostObjectRequest{
      bucketName = authResponse.body.bucket,
      header = ossHeader,
    };
    ossClient.postObject(uploadRequest, ossRuntime);
    generalRecognitionReq.picContent = `http://${authResponse.body.bucket}.${authResponse.body.endpoint}/${authResponse.body.objectKey}`;
  }
  var generalRecognitionResp = generalRecognitionWithOptions(generalRecognitionReq, runtime);
  return generalRecognitionResp;
}

model ImageAmazonRequest {
  gif?: boolean(name='Gif'),
  imgUrlList?: string(name='ImgUrlList'),
  instanceName?: string(name='InstanceName'),
  templateMode?: string(name='TemplateMode'),
  textList?: [ string ](name='TextList'),
}

model ImageAmazonResponseBody = {
  code?: int32(name='Code'),
  data?: {
    videoUrl?: string(name='VideoUrl'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model ImageAmazonResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: ImageAmazonResponseBody(name='body'),
}

async function imageAmazonWithOptions(request: ImageAmazonRequest, runtime: Util.RuntimeOptions): ImageAmazonResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.gif)) {
    body['Gif'] = request.gif;
  }
  if (!Util.isUnset(request.imgUrlList)) {
    body['ImgUrlList'] = request.imgUrlList;
  }
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.templateMode)) {
    body['TemplateMode'] = request.templateMode;
  }
  var bodyFlat : map[string]any = {};
  if (!Util.isUnset(request.textList)) {
    bodyFlat['TextList'] = request.textList;
  }
  body = {
    ...body,
    ...OpenApiUtil.query(bodyFlat),
  };

  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'ImageAmazon',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function imageAmazon(request: ImageAmazonRequest): ImageAmazonResponse {
  var runtime = new Util.RuntimeOptions{};
  return imageAmazonWithOptions(request, runtime);
}

model ImageCategoryRequest {
  instanceName?: string(name='InstanceName'),
  picUrl?: string(name='PicUrl'),
}

model ImageCategoryResponseBody = {
  code?: int32(name='Code'),
  data?: {
    cateResult?: [ 
      {
        id?: long(name='Id'),
        name?: string(name='Name'),
        score?: float(name='Score'),
      }
    ](name='CateResult'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model ImageCategoryResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: ImageCategoryResponseBody(name='body'),
}

async function imageCategoryWithOptions(request: ImageCategoryRequest, runtime: Util.RuntimeOptions): ImageCategoryResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picUrl)) {
    body['PicUrl'] = request.picUrl;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'ImageCategory',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function imageCategory(request: ImageCategoryRequest): ImageCategoryResponse {
  var runtime = new Util.RuntimeOptions{};
  return imageCategoryWithOptions(request, runtime);
}

model ImageDuplicationRequest {
  imageHeight?: long(name='ImageHeight'),
  imageWidth?: long(name='ImageWidth'),
  instanceName?: string(name='InstanceName'),
  outputImageNum?: long(name='OutputImageNum'),
  picNumList?: string(name='PicNumList'),
  picUrlList?: string(name='PicUrlList'),
}

model ImageDuplicationResponseBody = {
  code?: int32(name='Code'),
  data?: [ string ](name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model ImageDuplicationResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: ImageDuplicationResponseBody(name='body'),
}

async function imageDuplicationWithOptions(request: ImageDuplicationRequest, runtime: Util.RuntimeOptions): ImageDuplicationResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.imageHeight)) {
    body['ImageHeight'] = request.imageHeight;
  }
  if (!Util.isUnset(request.imageWidth)) {
    body['ImageWidth'] = request.imageWidth;
  }
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.outputImageNum)) {
    body['OutputImageNum'] = request.outputImageNum;
  }
  if (!Util.isUnset(request.picNumList)) {
    body['PicNumList'] = request.picNumList;
  }
  if (!Util.isUnset(request.picUrlList)) {
    body['PicUrlList'] = request.picUrlList;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'ImageDuplication',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function imageDuplication(request: ImageDuplicationRequest): ImageDuplicationResponse {
  var runtime = new Util.RuntimeOptions{};
  return imageDuplicationWithOptions(request, runtime);
}

model ImagePropertyRequest {
  instanceName?: string(name='InstanceName'),
  picUrl?: string(name='PicUrl'),
}

model ImagePropertyResponseBody = {
  code?: int32(name='Code'),
  data?: {
    propertyResults?: [ 
      {
        propertyId?: string(name='PropertyId'),
        propertyName?: string(name='PropertyName'),
        values?: [ 
          {
            probability?: float(name='Probability'),
            valueId?: string(name='ValueId'),
            valueName?: string(name='ValueName'),
          }
        ](name='Values'),
      }
    ](name='PropertyResults'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model ImagePropertyResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: ImagePropertyResponseBody(name='body'),
}

async function imagePropertyWithOptions(request: ImagePropertyRequest, runtime: Util.RuntimeOptions): ImagePropertyResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picUrl)) {
    body['PicUrl'] = request.picUrl;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'ImageProperty',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function imageProperty(request: ImagePropertyRequest): ImagePropertyResponse {
  var runtime = new Util.RuntimeOptions{};
  return imagePropertyWithOptions(request, runtime);
}

model ImageSegmentationRequest {
  instanceName?: string(name='InstanceName'),
  picContent?: string(name='PicContent'),
  returnPicFormat?: string(name='ReturnPicFormat'),
  returnPicType?: string(name='ReturnPicType'),
}

model ImageSegmentationAdvanceRequest {
  instanceName?: string(name='InstanceName'),
  picContentObject?: readable(name='PicContent'),
  returnPicFormat?: string(name='ReturnPicFormat'),
  returnPicType?: string(name='ReturnPicType'),
}

model ImageSegmentationResponseBody = {
  code?: int32(name='Code'),
  data?: {
    picUrl?: string(name='PicUrl'),
  }(name='Data'),
  message?: string(name='Message'),
  requestId?: string(name='RequestId'),
  success?: boolean(name='Success'),
}

model ImageSegmentationResponse = {
  headers: map[string]string(name='headers'),
  statusCode: int32(name='statusCode'),
  body: ImageSegmentationResponseBody(name='body'),
}

async function imageSegmentationWithOptions(request: ImageSegmentationRequest, runtime: Util.RuntimeOptions): ImageSegmentationResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  if (!Util.isUnset(request.returnPicFormat)) {
    body['ReturnPicFormat'] = request.returnPicFormat;
  }
  if (!Util.isUnset(request.returnPicType)) {
    body['ReturnPicType'] = request.returnPicType;
  }
  var req = new OpenApi.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'ImageSegmentation',
    version = '2021-01-20',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function imageSegmentation(request: ImageSegmentationRequest): ImageSegmentationResponse {
  var runtime = new Util.RuntimeOptions{};
  return imageSegmentationWithOptions(request, runtime);
}

async function imageSegmentationAdvance(request: ImageSegmentationAdvanceRequest, runtime: Util.RuntimeOptions): ImageSegmentationResponse {
  // Step 0: init client
  var accessKeyId = @credential.getAccessKeyId();
  var accessKeySecret = @credential.getAccessKeySecret();
  var securityToken = @credential.getSecurityToken();
  var credentialType = @credential.getType();
  var openPlatformEndpoint = @openPlatformEndpoint;
  if(Util.isUnset(openPlatformEndpoint)) {
    openPlatformEndpoint ='openplatform.aliyuncs.com';
  }
  if(Util.isUnset(credentialType)) {
    credentialType ='access_key';
  }
  var authConfig = new OpenApi.Config{
    accessKeyId = accessKeyId,
    accessKeySecret = accessKeySecret,
    securityToken = securityToken,
    type = credentialType,
    endpoint = openPlatformEndpoint,
    protocol = @protocol,
    regionId = @regionId,
  };
  var authClient = new OpenPlatform(authConfig);
  var authRequest = new OpenPlatform.AuthorizeFileUploadRequest{
    product = 'ImageSearch',
    regionId = @regionId,
  };
  var authResponse = new OpenPlatform.AuthorizeFileUploadResponse{};
  var ossConfig = new OSS.Config{
    accessKeySecret = accessKeySecret,
    type = 'access_key',
    protocol = @protocol,
    regionId = @regionId,
  };
  var ossClient : OSS = null;

  var fileObj = new FileForm.FileField{};
  var ossHeader = new OSS.PostObjectRequest.header{};
  var uploadRequest = new OSS.PostObjectRequest{};
  var ossRuntime = new OSSUtil.RuntimeOptions{};
  OpenApiUtil.convert(runtime, ossRuntime);
  var imageSegmentationReq = new ImageSegmentationRequest{};
  OpenApiUtil.convert(request, imageSegmentationReq);

  if(!Util.isUnset(request.picContentObject)) {
    authResponse = authClient.authorizeFileUploadWithOptions(authRequest, runtime);
    ossConfig.accessKeyId = authResponse.body.accessKeyId;
    ossConfig.endpoint = OpenApiUtil.getEndpoint(authResponse.body.endpoint, authResponse.body.useAccelerate, @endpointType);
    ossClient = new OSS(ossConfig);

    fileObj = new FileForm.FileField{
      filename = authResponse.body.objectKey,
      content = request.picContentObject,
      contentType = '',
    };
    ossHeader = new OSS.PostObjectRequest.header{
      accessKeyId = authResponse.body.accessKeyId,
      policy = authResponse.body.encodedPolicy,
      signature = authResponse.body.signature,
      key = authResponse.body.objectKey,
      file = fileObj,
      successActionStatus = '201',
    };
    uploadRequest = new OSS.PostObjectRequest{
      bucketName = authResponse.body.bucket,
      header = ossHeader,
    };
    ossClient.postObject(uploadRequest, ossRuntime);
    imageSegmentationReq.picContent = `http://${authResponse.body.bucket}.${authResponse.body.endpoint}/${authResponse.body.objectKey}`;
  }
  var imageSegmentationResp = imageSegmentationWithOptions(imageSegmentationReq, runtime);
  return imageSegmentationResp;
}

